package com.jambo.yudada.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jambo.yudada.model.dto.question.QuestionContentDTO;
import com.jambo.yudada.model.entity.App;
import com.jambo.yudada.model.entity.Question;
import com.jambo.yudada.model.entity.ScoringResult;
import com.jambo.yudada.model.entity.UserAnswer;
import com.jambo.yudada.model.vo.QuestionVO;
import com.jambo.yudada.service.QuestionService;
import com.jambo.yudada.service.ScoringResultService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
@Component
@ScoringStrategyConfig(appType = 0,scoringStrategy = 0)

public class CustomScoreScoringStrategy implements ScoringStrategy{
    @Resource
    private QuestionService questionService;
    @Resource
    private ScoringResultService scoringResultService;
    /**
     * 自定义测评类应用评分策略
     * @param choices
     * @param app
     * @return
     * @throws Exception
     */
    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        //1.根据id查询到题目和题目结果信息（按分数的降序排序）
        Question question=questionService.getOne(
                Wrappers.lambdaQuery(Question.class)
                        .eq(Question::getAppId,app.getId()));
        List<ScoringResult>scoringResultList=scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId,app.getId())
                        .orderByDesc(ScoringResult::getResultScoreRange));
        //2.统计用户的总得分
        int totalScore=0;
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();
        //遍历题目列表
        for (QuestionContentDTO questionContentDTO : questionContent) {
            //遍历答案列表
            for (String answer : choices) {
                //遍历题目中的选项
                for (QuestionContentDTO.Option option : questionContentDTO.getOptions()) {
                    //如果答案与选项匹配
                    if (option.getKey().equals(answer)) {
                       int score= Optional.ofNullable(option.getScore()).orElse(0);
                       totalScore+=score;
                    }
                }
            }
        }
        //3.遍历得分结果，找到第一个用户分数大于得分范围的结果，作为最终结果
        ScoringResult scoringResult = scoringResultList.get(0);
        for (ScoringResult scoringResult1 : scoringResultList) {
        if(totalScore>=scoringResult1.getResultScoreRange()){
            scoringResult=scoringResult1;
            break;
        }
        }
        //4.构造结果值，填充结果对象属性
        UserAnswer userAnswer=new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(scoringResult.getId());
        userAnswer.setResultName(scoringResult.getResultName());
        userAnswer.setResultDesc(scoringResult.getResultDesc());
        userAnswer.setResultPicture(scoringResult.getResultPicture());
        userAnswer.setResultScore(totalScore);
        return userAnswer;
    }
}
